
 
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "mythread.h"
#include "myaio.h"

#define NUM_THREADS	3
#define BUF_SIZE 200

/* Number threads */
int numThrd = 0;

/*This is a thread to print a simple message */
void *printSimpleMessage(void *num){
	int *inputNum = (int *) num;

    printf("Hello, my thread ID is: %d\n", *inputNum);
	numThrd++;
   
    mythread_exit(NULL);
    return 0;
}

/*This is the thread to print information read from keyboard */
void *readMessage(void *num){
	int *inputNum = (int *) num;

    printf("Hello, my thread ID is: %d\n", *inputNum);
	numThrd++;
	
	char buf[BUF_SIZE];
	printf("Enter input to be echoed:\n");
	int size = (int) read(0, buf, BUF_SIZE);
	printf("User typed: %s\n", buf);

    mythread_exit(NULL);
    return 0;
}

/**
 * Main function, sets up the threads
 *
 *@param argc  Number of arguments
 *@param argv  Arguments to the function
 *@return  Integer indicating proper or improper exit
 */
int main (int argc, char *argv[])
{
    mythread_t thread[NUM_THREADS];
    int tNum = 0;
    int errorCheck;

    /* Create the threads for printing messages */
	errorCheck = mythread_create(&thread[tNum], NULL, printSimpleMessage, (void *) &numThrd);
	if (errorCheck)
	{
		printf("ERROR; return code from mythread_create() for thread %d is %d----", tNum, errorCheck);
		exit(-1);
    }
	
	tNum++;
    errorCheck = mythread_create(&thread[tNum], NULL, readMessage, (void *) &numThrd);
	if (errorCheck)
	{
		printf("ERROR; return code from mythread_create() for thread %d is %d----", tNum, errorCheck);
		exit(-1);
    }
   
    /* Join the threads */
   void *status;
    for(tNum = 0; tNum < NUM_THREADS; tNum++)
    {
		errorCheck = mythread_join(thread[tNum], &status);
		if (errorCheck)
		{
			printf("ERROR; return code from mythread_join() for thread %d is %d----", tNum, errorCheck);
			exit(-1);
        }
    } 
   
	mythread_exit(NULL);

	return 0;
}
